﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace EstateNuke.Data
{
    public partial class CustomerLeaseRequirement
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        internal IList<CustomerLeaseRequirementFacility> CustomerLeaseRequirementFacilities
        {
            get
            {
                return InternalCustomerLeaseRequirementFacilities.Where(customerLeaseRequirementFacility => customerLeaseRequirementFacility.IsDeleted == false).ToList();
            }
        }

        /// <summary>
        /// </summary>
        public IList<Facility> Facilities
        {
            get
            {
                return InternalCustomerLeaseRequirementFacilities.Where(customerLeaseRequirementFacility => customerLeaseRequirementFacility.IsDeleted == false && customerLeaseRequirementFacility.Facility.IsDeleted == false).Select(customerLeaseRequirementFacility => customerLeaseRequirementFacility.Facility).ToList();
            }
        }

        #endregion

        #region Add & Remove CustomerLeaseRequirementFacility

        /// <summary>
        /// </summary>
        /// <returns></returns>
        internal CustomerLeaseRequirementFacility AddCustomerLeaseRequirementFacility(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var customerLeaseRequirementFacility = new CustomerLeaseRequirementFacility
                                                       {
                                                           RowId = Guid.NewGuid(),
                                                           CreatedDate = DateTime.Now,
                                                       };
            InternalCustomerLeaseRequirementFacilities.Add(customerLeaseRequirementFacility);
            customerLeaseRequirementFacility.IsDeleted = false;
            customerLeaseRequirementFacility.ModifiedDate = DateTime.Now;
            customerLeaseRequirementFacility.ModifiedByUser = modifiedUser;
            return customerLeaseRequirementFacility;
        }

        /// <summary>
        /// </summary>
        /// <param name = "customerLeaseRequirementFacility"></param>
        /// <param name = "modifiedUser"></param>
        internal void RemoveCustomerLeaseRequirementFacility(CustomerLeaseRequirementFacility customerLeaseRequirementFacility, User modifiedUser)
        {
            if (customerLeaseRequirementFacility == null)
            {
                throw new ArgumentNullException("customerLeaseRequirementFacility");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalCustomerLeaseRequirementFacilities.Contains(customerLeaseRequirementFacility))
            {
                customerLeaseRequirementFacility.Delete(modifiedUser);
            }
        }

        #endregion

        #region Set Facilities

        /// <summary>
        /// </summary>
        /// <param name = "facilities"></param>
        /// <param name = "modifiedUser"></param>
        public void SetFacilities(IList<Facility> facilities, User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (facilities != null)
            {
                var customerLeaseRequirementFacilities = InternalCustomerLeaseRequirementFacilities.ToList();
                customerLeaseRequirementFacilities.ForEach(customerLeaseRequirementFacility => customerLeaseRequirementFacility.Delete(modifiedUser));
                foreach (var facility in facilities.Distinct())
                {
                    var currentCustomerLeaseRequirementFacility = customerLeaseRequirementFacilities.FirstOrDefault(customerLeaseRequirementFacility => customerLeaseRequirementFacility.Facility == facility);
                    if (currentCustomerLeaseRequirementFacility != null)
                    {
                        currentCustomerLeaseRequirementFacility.CancelDelete(modifiedUser);
                    }
                    else
                    {
                        currentCustomerLeaseRequirementFacility = AddCustomerLeaseRequirementFacility(modifiedUser);
                        currentCustomerLeaseRequirementFacility.Facility = facility;
                    }
                }
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<CustomerLeaseRequirementFacility>) CustomerLeaseRequirementFacilities).ForEach(customerLeaseRequirementFacility => customerLeaseRequirementFacility.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}